<?php
defined('BASEPATH') OR exit('No direct script access allowed');

//预约信息
class Yuyue extends MY_Controller {

	function __construct(){
		parent::__construct();
		
		$this->stime = '08:00';
		$this->etime = '22:00';
	}	

	//start
	/**
	* @name 获取当日课程
	* @param status true int 0.自己的,1.其他教练的课程
	* @return 返回数组对象
	*/
	//end
	function get_yuyue(){
		$user_id = $this->user_id;
		$shop_id = $this->shop_id;
		
		$this->load->model('Yuyue/AppointModel','Appoint');
		$this->load->model('Index/ShopModel','shop');
		$this->load->model('Yuyue/CourseModel','Course');

		$status = $this->input->post('status',null,0);
		
		
		$list = $this->Course->get_list($shop_id,$user_id,$status);
		
		$type_arr = array('私教','团教');
		
		$this->load->model('User/CoachModel','CoachModel');
		
		foreach($list as $key=>$val){
			//$list[$key]['yuyue_num'] = $this->Appoint->get_count($val['id'],$date);
			$shop = $this->shop->_id_get_info($val['shop_id']);
			$list[$key]['shop_name'] = $shop['name'];
			$list[$key]['type_name'] = $type_arr[$val['type_id']];
			
			$list[$key]['stime'] = $this->stime;
			$list[$key]['etime'] = $this->etime;
			
			$list[$key]['coach_name'] = '';
			$coach = $this->CoachModel->get_info_c($val['cid']);
			if($coach){
				$list[$key]['coach_name'] = $coach['nickname'];
			}
		}
		
		$this->data_out($list);
	}
	
	
	//start
	/**
	* @name 获取课程预约信息
	* @param cid true int 课程ID
	* @param date true char 日期
	* @return 返回数组对象
	*/
	//end
	function get_yuyue_info(){
		$user_id = $this->user_id;
		
		$cid = $this->input->post('cid',null,0);
		$date = $this->input->post('date',null,date('Y-m-d'));
		
		
		$this->load->model('Yuyue/CourseModel','Course');
		$this->load->model('Yuyue/AppointModel','Appoint');
		
		$this->load->model('User/CoachModel','Coach');
		$this->load->model('Index/ShopModel','shop');
		
		$info = $this->Course->get_info($user_id,$cid);
		
		
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		$arr = array();
		$type_arr = array('私教','团教');
		$week_arr = array('周日','周一','周二','周三','周四','周五','周六');
		
		$arr['type_name'] = $type_arr[$info['type_id']];
		$arr['name'] = $info['name'];
		$arr['title'] = $info['title'];
		$arr['theme_img'] = $info['theme_img'];
		$arr['cid'] = $info['cid'];
		$arr['duration'] = $info['duration'];
		$arr['max_num'] = $info['max_num'];
		
		
		
		$arr['sdate'] = $date;
		$week = date('w',strtotime($arr['sdate']));
		$arr['week'] = $week_arr[$week];
		

		
		$arr['stime'] = $this->stime;
		$arr['etime'] = $this->etime;
		
		$shop = $this->shop->_id_get_info($info['shop_id']);
		$arr['shop_name'] = $shop['name'];
		
		$coach = $this->Coach->get_info($info['cid']);
		if($coach){
			$arr['coach'] = $coach;
		}
		
		
		$time = time();
		
		$yuyue_list = $this->Appoint->get_list($cid,$date);
		
		$ss = 2 *3600;
		foreach($yuyue_list as $key=>$val){
			$yuyue_list[$key]['is_yuyue'] = 1;
			$stime = strtotime("{$val['sdate']} {$val['stime']}");
			if($time>=$stime-$ss){
				$yuyue_list[$key]['is_yuyue'] = 0;
			}
		}
		$arr['yuyue_list'] = $yuyue_list;
		//$arr['yuyue_num'] = count($yuyue_list);
		
		$this->data_out($arr);
	}
	
	//start
	/**
	* @name 获取课程预约时间
	* @param cid true int 课程ID
	* @return 返回数组对象
	*/
	//end
	function get_yuyue_time(){
		$user_id = $this->user_id;
		
		$cid = $this->input->post('cid',null,0);
		
		$this->load->model('Yuyue/CourseModel','Course');
		$this->load->model('Yuyue/AppointModel','Appoint');
		$this->load->model('User/CoachModel','Coach');
		
		$info = $this->Course->get_info($user_id,$cid);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		
		$start = $this->time_to_minute($this->stime);
		$end = $this->time_to_minute($this->etime);
		
		$end = $end - $info['duration'];
		
		$interval = $info['duration']+10;
		
		
		$arr = array();
		for($i=$start;$i<$end;$i+=$interval){
			$arr[] = $this->minute_to_time($i);
		}
		
		$this->data_out($arr);
	}
	
	//start
	/**
	* @name 添加预约
	* @param cid true int 课程ID
	* @param date true char 预约日期2018-06-23
	* @param stime true char 预约时间08:00
	* @param uid true int 预约会员
	* @param card_id true int 会员卡ID
	* @param coach_id true int 教练ID
	* @return 返回数组对象
	*/
	//end
	function add_yuyue(){
		$user_id = $this->user_id;

		$cid = $this->input->post('cid',null,0);
		$date = $this->input->post('date',null,'');
		$stime = $this->input->post('stime',null,'');
		$uid = $this->input->post('uid',null,0);
		$card_id = $this->input->post('card_id',null,0);
		$coach_id = $this->input->post('coach_id',null,0);
		
		
		$this->load->model('Yuyue/CourseModel','Course');
		$this->load->model('Yuyue/AppointModel','Appoint');
		$this->load->model('User/CoachModel','Coach');
		$this->load->model('User/MemberCardModel','card');
		
		
		$info = $this->Course->get_info($user_id,$cid);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		
		
		$start = $this->time_to_minute($this->stime);
		$end = $this->time_to_minute($this->etime);
		$end = $end - $info['duration'];
		
		$mm = $this->time_to_minute($stime);
		if($mm<$start || $mm>=$end){
			$this->err_out('超出课程时间之外');
		}
		
		$is_exits = $this->Appoint->time_is_exits($cid,$date,$uid,$stime);
		if($is_exits){
			$this->err_out('你已经预约过该时间了');
		}
		$yuyue_num = $this->Appoint->get_count($cid,$date,$stime);
		if($yuyue_num>=$info['max_num']){
			$this->err_out('该课程已经满员');
		}
		
		$card = $this->card->get_info($uid,$card_id);
		if(!$card){
			$this->err_out('会员卡不存在');
		}
		if($card['num']<=0){
			$this->err_out('会员卡已经次数不足');
		}
		
		if(empty($coach_id)){
			$coach_id = $user_id;
		}
		
		$time = time();
		$arr = array(
			'user_id'=>$uid,
			'cid'=>$info['id'],
			'mid'=>$coach_id,
			'shop_id'=>$info['shop_id'],
			'sdate'=>$date,
			'stime'=>$stime,
			'num'=>1,
			'card_id'=>$card_id,
			'addtime'=>$time
		);
		
		$result = $this->card->reduce($uid,$card_id,1);
		if(!$result){
			$this->err_out($this->card->getError(0));
		}
		
		$result = $this->Appoint->add_data($arr);

		$this->data_out($arr,"预约成功");
	}
	
	//start
	/**
	* @name 根据时间获取预约人数
	* @param cid true int 课程ID
	* @param date true int 日期（格式：2018-06-12）
	* @param stime true int 上课时间
	* @return 返回预约总人数
	*/
	//end
	function time_get_yuyue(){
		$user_id = $this->user_id;
		$cid = $this->input->post('cid',null,'');
		$date = $this->input->post('date',null,'');
		$stime = $this->input->post('stime',null,0);
		
		
		$this->load->model('Yuyue/CourseModel','Course');
		
		$info = $this->Course->get_info($user_id,$cid);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		$this->load->model('Yuyue/AppointModel','Appoint');
		
		$num = $this->Appoint->get_count($cid,$date,$stime);
		
		$this->data_out($num);
	}
	
	//start
	/**
	* @name 预约签到
	* @param aid true int 预约的ID
	* @return 返回数组对象
	*/
	//end
	function signin(){
		$user_id = $this->user_id;
		$aid = $this->input->post('aid',null,0);

		$this->load->model('Yuyue/AppointModel','Appoint');
		
		$info = $this->Appoint->get_info($user_id,$aid);
		if(!$info){
			$this->err_out('该预约不存在');
		}
		if($info['status']!=0){
			$this->err_out('该预约已经处理过');
		}
		
		$time = time();
		$this->Appoint->edit(array('status'=>1,'change_time'=>$time),"id='{$aid}'");
		
		$this->data_out(1,"签到成功");
	}
	
	//start
	/**
	* @name 取消预约
	* @param aid true int 预约的ID
	* @return 返回数组对象
	*/
	//end
	function cancel(){
		$user_id = $this->user_id;
		$aid = $this->input->post('aid',null,0);
		
		$this->load->model('Yuyue/AppointModel','Appoint');
		
		$info = $this->Appoint->get_info($user_id,$aid);
		if(!$info){
			$this->err_out('该预约不存在');
		}
		if($info['status']!=0){
			$this->err_out('该预约已经处理过');
		}

		
		$this->load->model('User/MemberCardModel','card');
		$result = $this->card->addto($info['user_id'],$info['card_id'],1);
		if(!$result){
			$this->err_out($this->card->getError(0));
		}
		
		$time = time();
		$this->Appoint->edit(array('status'=>2,'change_time'=>$time),"id='{$aid}'");
		
		$this->data_out(1,"取消预约成功");
	}
	
	function time_to_minute($timer){
		$fg = explode(':',$timer);
		
		if(count($fg)<2){
			$h = intval($fg[0]);
			$m = 0;
		}
		else{
			$h = intval($fg[0]);
			$m = intval($fg[1]);
		}
		$result = intval($h * 60)+$m;
		return $result;
	}
	function minute_to_time($minute){
		$h = intval($minute/60);
		$m = $minute%60;
		
		$h = $h<10?'0'.$h:$h;
		$m = $m<10?'0'.$m:$m;
		
		$result = $h.':'.$m;
		return $result;
	}
}
